% Load the Excel file from Fejer, with the values of
% Scherliess-Fejer model (1999). Idea is to create a 5D
% array which then can be used to evaulate the model
% for any local time, date , longitude and solar flux (f10.7)
% The xls files are
% FinalModel100.xls  FinalModel140.xls  FinalModel180.xls  
% FinalModel110.xls  FinalModel150.xls  FinalModel190.xls  
% FinalModel120.xls  FinalModel160.xls  FinalModel200.xls  
% FinalModel130.xls  FinalModel170.xls 

filepath = '/home/mnair/projects/ace_tensor/ScherliessFejer1999Model/';

s = dir([filepath '*.xls']);

for i=1:length(s),
    
    [a] = xlsread([filepath s(i).name]);
    K = sum(isnan(a),2);
    a(K==25,:) = [];
    
    b(1,:,:) = a(2:59,2:end); % Nov-Feb 1
    b(2,:,:)= a(61:118,2:end); % Equinox (Mar-Apr) 4
    b(3,:,:) = a(120:177,2:end); % May - Aug 7
    b(4,:,:) = a(61:118,2:end); % Equinox (Sep-Oct) 10
    b(5,:,:) = a(2:59,2:end); % Nov-Feb 13
    b(6,:,:)= a(61:118,2:end); % Equinox (Mar-Apr) 4
        
    for j = 1:6,
        
    Fejer1999(i,j,2:59,2:25) = b(j,:,:);
    Fejer1999(i,j,1,2:25) = b(j,58,:);
    Fejer1999(i,j,60,2:25) = b(j,1,:);
    Fejer1999(i,j,:,1) = Fejer1999(i,j,:,25);
    Fejer1999(i,j,:,26) = Fejer1999(i,j,:,2);
    
    end;
    
            
end;

%%
dim1_f107 = [100,110,120,130,140,150,160,170,180,190,200];
%Dec Equinox, Mar Soltice, June Solstice, Sep Equinox, Dec Soltstice 
dim2_season = [-0.0274, 0.2164, 0.4685, 0.7260, 0.9726,1.2164 ];
dim3_lt = [-0.50,0, 0.50,  1.00,  1.50,  2.00,  2.50,  3.00,  3.50,  4.00,  4.50,  5.00,  5.50,  6.00,  6.50,  7.00,  7.50,  8.00,  8.50,  9.00,  9.50, 10.00, 10.50, 11.00, 11.50, 12.00, 12.50, 13.00, 13.50, 14.00, 14.50, 15.00, 15.50, 16.00, 16.50, 17.00, 17.25, 17.50, 17.75, 18.00, 18.25, 18.50, 18.75, 19.00, 19.25, 19.50, 19.75, 20.00, 20.25, 20.50, 20.75, 21.00, 21.25, 21.50, 21.75, 22.00, 22.50, 23.00, 23.50, 24.0];
dim4_longitude = [-195.0 -180.00 -165.00 -150.00 -135.00 -120.00 -105.00 -90.00 -75.00 -60.00 -45.00 -30.00 -15.00  0.00 15.00 30.00 45.00 60.00 75.00 90.00 105.00 120.00 135.00 150.00 165.00 180.00];


%% Test the model

%interpn(dim1_f107,dim2_season,dim3_lt,dim4_longitude, Fejer1999, ...
%    dim1_f107(1),dim2_season(1),dim3_lt(1),dim4_longitude(1));

long = 0;
season =  0.6; % March 22
f107 = 100;

numb=1; for i = 0: (5/60) : 48,
    lt = i + (long/15);
    if lt < 0, lt = lt +24;end; 
    if lt > 24, lt = lt - 24;end;
    
fejer(numb)  = fejer1999(f107, season , lt , long);
numb=numb+1;
end;plot(fejer);
%%

fid = fopen('/home/mnair/projects/ace_tensor/acedata/fejer_1999.txt','wt');


for i = 1:11 , % f10.7
    fprintf(fid,'{ //f10.7 = %d \n',dim1_f107(i));
    for j = 1:6, % seasons
        fprintf(fid,'{ //month = %d \n',dim2_season(j));
        for k = 1:60, %lt
            fprintf(fid,'//lt = %6.2f \n {',dim3_lt(k));
            for l = 1:26, %longitude
                             
                fprintf(fid,'% 7.3f,', Fejer1999(i,j,k,l));
            end;
            fprintf(fid,'},\n');
        end;
        fprintf(fid,'},\n');
     end;
     fprintf(fid,'},\n');
end;


fid = fopen('/home/mnair/projects/ace_tensor/acedata/fejer_1999.txt','wt');


for i = 1:11 , % f10.7
    
    for j = 1:6, % seasons
    
        for k = 1:60, %lt
    
            for l = 1:26, %longitude
    
                fprintf(fid,'% 7.3f ', Fejer1999(i,j,k,l));
            end;
            fprintf(fid,'\n');
        end;
        
     end;
     
end;
                

fclose(fid);
%%

fid = fopen('/home/mnair/projects/ace_tensor/acedata/fejer_1999.bin','wb');


for i = 1:11 , % f10.7
    
    for j = 1:6, % seasons
    
        for k = 1:60, %lt
    
            for l = 1:26, %longitude
    
                %fwrite(fid, int32(Fejer1999(i,j,k,l)*1e3),'int32');
                fwrite(fid, Fejer1999(i,j,k,l),'double');
            end;
           
        end;
        
     end;
     
end;
                

fclose(fid);

